package top.dark.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.dark.common.exception.BizCodeEnum;
import top.dark.member.entity.MemberEntity;
import top.dark.member.exception.PhoneExistException;
import top.dark.member.exception.UsernameExistException;
import top.dark.member.feign.CouponFeign;
import top.dark.member.service.MemberService;
import top.dark.common.utils.PageUtils;
import top.dark.common.utils.R;
import top.dark.member.vo.RegisterVo;
import top.dark.member.vo.SocialGithubUserVo;
import top.dark.member.vo.UserLoginVo;

/**
 * 会员
 * @author lijiao
 * @email DarknessShadow11@outlook.com
 * @date 2021-02-01 17:27:23
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponFeign couponFeign;

    /**
     * 社交github登录,登录和注册合并逻辑
     */
    @RequestMapping("/oauth/github/login")
    public R oauthGithubLogin(@RequestBody SocialGithubUserVo vo) {
        MemberEntity entity = memberService.oauthGithubLogin(vo);
        if (entity != null) {
            return R.ok().put("data", entity);
        } else {
            return R.error();
        }
    }

    /**
     * 登录
     */
    @RequestMapping("/login")
    public R login(@RequestBody UserLoginVo vo) {
        MemberEntity entity = memberService.login(vo);
        if (entity != null) {
            return R.ok().put("data", entity);
        } else {
            return R.error();
        }
    }
    /**
     * 注册会员
     */
    @RequestMapping("/register")
    public R register(@RequestBody RegisterVo vo) {
        try {
            memberService.register(vo);
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.MSISDN_EXIT_EXCEPTION.getCode(), BizCodeEnum.MSISDN_EXIT_EXCEPTION.getMsg());
        } catch (Exception e) {
            return R.error(BizCodeEnum.UNKNOW_EXCEPTION.getCode(), BizCodeEnum.UNKNOW_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 获取用户已有券信息
     */
    @RequestMapping("/mycoupon")
    public R getCouponList(){
        return couponFeign.list(new HashMap<>());
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
